package com.kyist.frp.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * frp插件的配置文件类
 *
 * @Author kyist
 * @Date 2021/12/11 18:50
 */

public class FrpConfig implements Serializable {
    private String mode;
    private List<String> allow = new ArrayList<>();
    private List<String>  block = new ArrayList<>();
    public static Properties prop;
    public static final String SERIALIZEFILE = System.getProperty("user.dir")+System.getProperty("file.separator") +  "frpConf.json";

    public static void main(String[] args) {
        //获取当前路径
        System.out.println(System.getProperty("user.dir"));
        //获取文件分隔符
        System.out.println(System.getProperty("file.separator"));
    }
    static {
        //获取配置文件，与jar同目录的frpConfig.properties
        prop = new Properties();
        try {

            File file = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "frpConfig.properties");
            if (file.exists()) {
                prop.load(new FileReader(file));
                System.out.println("加载配置文件"+file.getPath());
            }else{
                ClassPathResource classPathResource = new ClassPathResource("frpConfig.properties");
                prop.load(classPathResource.getInputStream());
                System.out.println("加载默认配置文件: frpConfig.properties");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //初始化，将prop的属性取出，方便处理
    public void init(){
        setMode(prop.getProperty("IP_MODE","no"));
        setAllow(prop.getProperty("IP_ALLOW",""));
        setBlock(prop.getProperty("IP_BLOCK",""));
    }



    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public  List<String> getAllow() {
        return allow;
    }

    public void setAllow(String allow) {
        setIpList(this.allow,allow);
    }

    public void setAllow(List<String> allow) {
        this.allow.addAll(allow);
    }

    public List<String> getBlock() {
        return block;
    }

    public void setBlock(String block) {
        setIpList(this.block, block);
    }

    public void setBlock(List<String> block) {
        this.block.addAll(block);
    }

    private void setIpList(List<String> ips, String block) {
        block = block.replace("[","").replace("]","");
        if (StrUtil.isNotBlank(block)) {
            List<String> ipList = strToList(block);
            assert ipList != null;
            if (!ipList.isEmpty()) {
                ips.removeAll(ipList);
                ips.addAll(ipList);
            }
        }
    }


    /**
     * 将字符串转为字符串数组
     * @param str string
     * @param split 分隔符
     * @return []
     */
    private static String[] strToArr(String str, String split){
        if (StrUtil.isEmpty(str)) {
            return null;
        }else{
            String[] strs = str.split(split);
            for (int i = 0; i < strs.length; i++) {
                strs[i] = strs[i].trim();
            }
            return strs;
        }
    }



    /**
     * 将字符串转为字符串数组
     * @param str string
     * @return []
     */
    private static List<String> strToList(String str){
        if (StrUtil.isEmpty(str)) {
            return null;
        }else{
            List<String> list = new ArrayList<>();
            String[] strs = str.split(",");
            for (int i = 0; i < strs.length; i++) {
                list.add(strs[i].trim());
            }
            return list;
        }
    }

    @Override
    public String toString() {
        return "FrpConfig{" +
                "mode='" + mode + '\'' +
                ", allow=" + allow +
                ", block=" + block +
                '}';
    }
}
